package jp.sinya.temicontact.mvp.presenter;

import com.pubnub.api.PubNub;
import com.pubnub.api.callbacks.PNCallback;
import com.pubnub.api.models.consumer.PNPublishResult;
import com.pubnub.api.models.consumer.PNStatus;

import jp.sinya.temicontact.MyApplication;
import jp.sinya.temicontact.mvp.model.Contact;
import jp.sinya.temicontact.mvp.model.Message;
import jp.sinya.temicontact.mvp.view.ContactDetailView;
import jp.sinya.temicontact.utils.LogUtils;
import jp.sinya.temicontact.utils.PubnubUtils;

import static jp.sinya.temicontact.constants.Constants.CHANNEL_NAME;

/**
 * @author Koizumi Sinya
 * @date 2018/01/11. 23:21
 * @edithor
 * @date
 */
public class ContactDetailPresenter {
    private ContactDetailView view;
    private Contact contact;

    //暂时考虑用设备id作为唯一
    public ContactDetailPresenter(ContactDetailView view, Contact contact) {
        this.view = view;
        this.contact = contact;
    }

    public Contact getContact() {
        return contact;
    }

    public void load() {
        view.loadData(contact);
    }

    public void sendMessage(String message) {
        Message msg = new Message(MyApplication.id, message);
        PubNub pubnub = PubnubUtils.getInstance().getPubNub();
        pubnub.publish() //
                .message(msg) //
                .channel(CHANNEL_NAME) //
                .async(new PNCallback<PNPublishResult>() {
                    @Override
                    public void onResponse(PNPublishResult result, PNStatus status) {
                        // handle publish result, status always present, result if successful
                        // status.isError to see if error happened

                        LogUtils.Sinya("onResponse..." + status);
                    }
                });
    }
}
